package com.andcard.action;

import java.util.List;

import com.andcard.AndCard;
import com.andcard.R;
import com.andcard.obj.Card;
import com.andcard.obj.DatabaseStats;
import com.andcard.sql.DbAdapter;
import com.andcard.util.Reference;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TabStats extends AbsAction implements OnClickListener{

	private TextView textviewStatsData;
	private Button btRefresh;
	private Button btAllCards;
	private Button btKoCards;
	
	public TabStats(AndCard activity, DbAdapter dbAdapter){
		super(activity, dbAdapter);
		
		textviewStatsData = (TextView)activity.findViewById(R.id.textviewStatsData);
		btRefresh = (Button)activity.findViewById(R.id.btRefresh);
		btAllCards = (Button)activity.findViewById(R.id.btAllCards);
		btKoCards = (Button)activity.findViewById(R.id.btKoCards);
		
		btRefresh.setOnClickListener(this);
		btAllCards.setOnClickListener(this);
		btKoCards.setOnClickListener(this);
		
		Log.d(Reference.TAG, "TabStats.Constructor");
	}
	
	public void onClick(View v) {
		
		Log.d(Reference.TAG, "TabStats.onClick - " + v.toString() );
		
		if (v.equals(btRefresh)){
			Log.d(Reference.TAG, "TabStats.onClick - btRefresh" );
			this.showDatabaseStats();
		}else if(v.equals(btAllCards)){
			Log.d(Reference.TAG, "TabStats.onClick - btAllCards" );
			this.showFilterCards(null);
		}else if(v.equals(btKoCards)){
			Log.d(Reference.TAG, "TabStats.onClick - btKoCards" );
			this.showFilterCards("KO");
		}
	}
	
	public void showDatabaseStats(){
		Log.d(Reference.TAG, "TabStats.showDatabaseStats");
		
		if (this.getDbAdapter().isDatabaseOpen() == false){
			this.getActivity().showDialog(Reference.ALERT_NO_DATABASE_OPEN);
			return;
		}
		
		DatabaseStats databaseStats = this.getDbAdapter().getDatabaseStats();
		
		if (databaseStats == null){
			this.getActivity().showDialog(Reference.ALERT_NO_DATABASE_STATS);
			return;
		}else{
			String strStats = "";
            strStats = "Database name: " + this.getDbAdapter().getPathDatabase() + "\n";
            strStats = strStats + "Read only: " + this.getDbAdapter().isDatabaseReadOnly() + "\n";
            strStats = strStats + "Reverse: " + this.getDbAdapter().isDatabaseReverse() + "\n";
            strStats = strStats + "Record counts: " + databaseStats.getCountRecords() + "\n";
            strStats = strStats + "Ok answers: " + databaseStats.getOkAnswers() + "\n";
            strStats = strStats + "Ko answers: " + databaseStats.getKoAnswers() + "\n\n";
            strStats = strStats + "Count answer/date:\n";
            
            for (int x = 0; x < databaseStats.getCountData().length; x++){
            	strStats = strStats + databaseStats.getCountData()[x].getDate() + " - " + databaseStats.getCountData()[x].getCount() + "\n";
            }
            
            this.textviewStatsData.setText(strStats);
		}
	}
	
	public void showFilterCards(String filter){
		
		Log.d(Reference.TAG, "TabStats.showCards -filter " + filter );
		
		if (this.getDbAdapter().isDatabaseOpen() == false){
			this.getActivity().showDialog(Reference.ALERT_NO_DATABASE_OPEN);
			return;
		}
		
		List<Card> listCard = this.getDbAdapter().getFilterCards(filter);
		
		if (listCard == null || listCard.size() == 0){
			this.getActivity().showDialog(Reference.ALERT_NO_DATABASE_STATS);
			return;
		}else{
			String strCards = "";
			for (int x = 0; x < listCard.size(); x++){
				strCards += "Q: " + listCard.get(x).getQuestion() + "\nA: " + listCard.get(x).getAnswer() + "\n"; 
			}
			
			this.textviewStatsData.setText(strCards);
		}
	}
}
